package co.baiku.boot.core.orm.tools;

import co.baiku.boot.common.exception.ApiException;
import co.baiku.boot.common.tools.ObjectTools;
import co.baiku.boot.core.orm.request.RequestFilter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

public class QueryTools {
    private Logger log = LoggerFactory.getLogger(QueryTools.class);

    public static <T> QueryWrapper<T> newQueryWrapper() {
        return new QueryWrapper<>();
    }

    public static <T> LambdaQueryWrapper<T> newLambdaQueryWrapper() {
        return new LambdaQueryWrapper<>();
    }

    public static <T> QueryWrapper<T> parseRequest(Map<String, ColumnCache> columnMap, Map<String, Object> search, Map<String, String> sortBy) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        Map<String, RequestFilter> parseResult = RequestFilter.parse(search);
        Collection<RequestFilter> filters = parseResult.values();
        for (RequestFilter filter : filters) {
            ColumnCache columnCache = columnMap.get(filter.fieldName.toUpperCase());
            if (columnCache == null) {
                throw ApiException.of("不支持的查询字段：" + filter.fieldName);
            }
            String fieldName = columnCache.getColumn();
            switch (filter.operator) {
                case EQ:
                    wrapper.eq(fieldName, filter.value);
                    break;
                case NE:
                    wrapper.ne(fieldName, filter.value);
                    break;
                case LLIKE:
                    wrapper.likeLeft(fieldName, filter.value);
                    break;
                case RLIKE:
                    wrapper.likeRight(fieldName, filter.value);
                    break;
                case LIKE:
                    wrapper.like(fieldName, filter.value);
                    break;
                case GT:
                    wrapper.gt(fieldName, filter.value);
                    break;
                case LT:
                    wrapper.lt(fieldName, filter.value);
                    break;
                case GTE:
                    wrapper.ge(fieldName, filter.value);
                    break;
                case LTE:
                    wrapper.le(fieldName, filter.value);
                    break;
                case ISNULL:
                    wrapper.isNull(fieldName);
                    break;
                case ISNOTNULL:
                    wrapper.isNotNull(fieldName);
                    break;
                case IN:
                    Collection value = (Collection) filter.value;
                    Iterator iterator = value.iterator();
                    if (value.size() == 1) {
                        //如果只有一个元素,则直接用EQ
                        wrapper.eq(fieldName, iterator.next());
                        break;
                    }
                    wrapper.in(fieldName, (Collection) filter.value);
                    break;
                case NIN:
                    Collection notInValue = (Collection) filter.value;
                    Iterator notInIterator = notInValue.iterator();
                    if (notInValue.size() == 1) {
                        //如果只有一个元素,则直接用EQ
                        wrapper.ne(fieldName, notInIterator.next());
                        break;
                    }
                    wrapper.notIn(fieldName, (Collection) filter.value);
                    break;
                default:
            }
        }
        if (ObjectTools.isNotBlank(sortBy)) {
            sortBy.forEach((k, v) -> wrapper.orderBy(true, "asc".equalsIgnoreCase(v), columnMap.get(k.toUpperCase()).getColumn()));
        }
        return wrapper;
    }
}
